import random
try:
    import numpy as np
except ImportError:
    np = None

class LotterySystem:
    # 初始化
    def __init__(self):
        self.participants = []
        self.probabilities = []
        self.winners = []
    # 增加参与者
    def add_participant(self, name, probability=None):
        self.participants.append(name)
        if probability is not None:
            self.probabilities.append(probability)
        else:
            self.probabilities.append(1.0/len(self.participants))
    # 抽奖
    def draw_winners(self, num_winners, use_probability=False):
        if num_winners > len(self.participants):
            raise ValueError("获奖者数量不能超过参与者数量")
        
        if use_probability and len(self.probabilities) == len(self.participants):
            if np is not None:
                # 使用numpy进行概率抽奖
                probabilities = np.array(self.probabilities)
                probabilities = probabilities / probabilities.sum()
                winner_indices = np.random.choice(
                    len(self.participants), 
                    size=num_winners, 
                    replace=False,
                    p=probabilities
                )
                self.winners = [self.participants[i] for i in winner_indices]
            else:
                # numpy不可用时回退到random模块实现
                print("警告: numpy模块未安装，使用random模块进行概率抽奖")
                weighted_list = []
                for i, prob in enumerate(self.probabilities):
                    weighted_list.extend([i] * int(prob * 1000))
                self.winners = [self.participants[i] for i in random.sample(weighted_list, num_winners)]
        else:
            # 默认随机抽奖
            self.winners = random.sample(self.participants, num_winners)
    # 获取获奖者
    def get_winners(self):
        return self.winners
    # 清空
    def clear_participants(self):
        self.participants = []
    # 清空获奖者
    def clear_winners(self):
        self.winners = []

if __name__ == "__main__":
    import sys
    lottery = LotterySystem()
    
    # 用户输入参与者
    print("请输入参与者名单(用逗号分隔):")
    try:
        names = sys.stdin.readline().strip().replace('，', ',').split(',')
        names = [name.strip() for name in names if name.strip()]
    except Exception as e:
        print(f"输入错误: {e}")
        sys.exit(1)
    
    # 用户输入概率(可选)
    print("是否要为每个参与者设置不同的中奖概率?(y/n)")
    try:
        use_prob = sys.stdin.readline().strip().lower() == 'y'
    except Exception as e:
        print(f"输入错误: {e}")
        sys.exit(1)
    
    if use_prob:
        print(f"请为{len(names)}个参与者输入概率(用逗号隔开，总和应为1):")
        try:
            probs = list(map(float, sys.stdin.readline().strip().replace('，', ',').split(',')))
            for name, prob in zip(names, probs):
                lottery.add_participant(name, prob)
        except Exception as e:
            print(f"输入错误: {e}")
            sys.exit(1)
    else:
        for name in names:
            lottery.add_participant(name)
    
    print("参与者名单:", lottery.participants)
    if use_prob:
        print("参与者概率:", lottery.probabilities)
    
    print("请输入要抽取的获奖者数量:")
    try:
        num_winners = int(sys.stdin.readline().strip())
    except Exception as e:
        print(f"输入错误: {e}")
        sys.exit(1)
    
    # 选择抽奖方式
    if use_prob:
        lottery.draw_winners(num_winners, use_probability=True)
    else:
        lottery.draw_winners(num_winners)
    
    print("获奖者:", lottery.get_winners())